package com.apadog.study.utils;

import com.microsoft.playwright.Playwright;

public class PlaywrightSingleton {

    // 私有构造函数，防止外部实例化
    private PlaywrightSingleton() {}

    // 静态内部类，延迟加载，线程安全
    private static class Holder {
        private static final Playwright INSTANCE = createPlaywright();

        private static Playwright createPlaywright() {
            Playwright playwright = Playwright.create();
            // 在 JVM 退出时自动关闭
            Runtime.getRuntime().addShutdownHook(new Thread(playwright::close));
            return playwright;
        }
    }

    // 外部调用方法
    public static Playwright getInstance() {
        return Holder.INSTANCE;
    }

    public static Playwright get(){
        Playwright playwright = Playwright.create();
        // 在 JVM 退出时自动关闭
        Runtime.getRuntime().addShutdownHook(new Thread(playwright::close));
        return playwright;
    }

}
